package model.market;
//AMLOW: more configuration on bank behaviour.
public class BankOfCredit {

	//Fraction of holding that investor needs to own..
	public static double LONG_MARGIN = .15;
	
	/**
	 * Used to determine investors' working capital when they are long..
	 * @param market
	 * @param pos
	 * @param desiredMarginBuffer
	 * @return
	 */
	public double determineUnallocatedWorkingCapitalLong(double[] marketPrices,
			Position pos, double desiredMarginBuffer) {

		double stockValue=0;
		for(int i=0;i<marketPrices.length;i++){
		
			int quantity = pos.getExecutedQuantity(i);
			stockValue += quantity * marketPrices[i];
		}
		// Can lever up but leave a buffer..
		double unallocatedWorkingCapital = 0;
		unallocatedWorkingCapital = getPossibleLongPosition(marketPrices,pos,desiredMarginBuffer)
				- stockValue;

		return unallocatedWorkingCapital;

	}


	
	public double getPossibleLongPosition(double[] marketPrices,
			Position pos, double desiredMarginBuffer){
		return getPossibleLongPosition(pos.getWorth(marketPrices),desiredMarginBuffer);
	}
	
	public double getPossibleLongPosition(double worth,double desiredMarginBuffer){
		return worth/(LONG_MARGIN+desiredMarginBuffer);
	}
	
	
	public boolean marginCall(double[] marketPrices, Position pos) {
		return determineUnallocatedWorkingCapitalLong(marketPrices, pos, 0) < 0;
		
	}
	
//AM: shorting is out.	
//	public double getShortedStockCashRequirement(MarketManager market,
//			Position pos, double desiredMarginBuffer){
//int absoluteQuantity = Math.abs(pos.getExecutedQuantity());
//double stockValue = absoluteQuantity * market.getMarketPrice();
//return ((stockValue)* (1 + SHORT_MARGIN +desiredMarginBuffer));
//}
//
//
//public double getPossibleShortPosition(MarketManager market,
//Position pos, double desiredMarginBuffer){
//
//return pos.getWorth(market.getMarketPrices())/(SHORT_MARGIN+desiredMarginBuffer);
//}
//	public double determineUnallocatedWorkingCapitalShort(MarketManager market,
//			Position pos, double desiredMarginBuffer) {
//
//		// can short but leave a buffer..
//		double workingCapital = (pos.getCash()-getShortedStockCashRequirement(market, pos, desiredMarginBuffer))
//											/(SHORT_MARGIN+desiredMarginBuffer);
//		
//		return workingCapital;
//		
//	}

	
	
	
}
